<%@ page contentType="text/html; charset=utf-8" language="java"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系统系统</title>
<link type="text/css" rel="stylesheet" href="../../css/index.css" />
<script language="javascript" src="../../js/jquery-1.7.1.min.js"></script>
<script language="javascript"  src="../../js/jquery.form.js"></script>
<script language="javascript" src="../../js/lightbox/jquery.lightbox_me.js"></script>
<script language="javascript" src="../../js/validate.js"></script>
</head>
<body>
<div class="right_main">
        <!-- 	<div class="right_title">监控管理 >  系统|服务管理</div>  -->
            <div class="right_main_title_bg">
            	<s:form  id="selectForm" name="selectForm"  action="selectFunctionShell" theme="simple">  
					<div class="right_main_padding_1">
						<span class="spanTxt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
						 <span class="spanTxt">应用名称：</span>
						 <s:select name="_server_id" list="#session.monitorServerList"  listKey="id"  listValue="server_name"   headerKey="" headerValue="--请选择应用--" />
	                     <span class="btn_1 searchBtn"  id="selectButton" onclick="selectData();" ></span>
	                    &nbsp;&nbsp;&nbsp;&nbsp;<span class="default_btn_2" onclick="javascript:toUploadDiv();">添加应用功能点</span>
	                    <span style="display: none"> 
							gotoPage<s:textfield name="gotoPage" id="gotoPage" /> 
							pageSize<s:textfield name="pageSize" id="pageSize" /> 
							firstPage<s:textfield name="firstPage" id="firstPage" /> 
							lastPage<s:textfield name="lastPage" id="lastPage" />
							nextPage<s:textfield name="nextPage" id="nextPage" />
							prePage<s:textfield name="prePage" id="prePage" /> 
							countPage<s:textfield name="countPage" id="countPage" />
							countData<s:textfield name="countData" id="countData" />
						</span>
                </div>
			    </s:form>
                <table class="table_1" cellpadding="0" cellspacing="1">
                	<tr class="th_bg">
                		<td>所属应用名</td>
                        <td>功能点名称</td>
                   		<td>对应监控脚本</td>
                   		<td>输入参数</td>
                   		<td>输出</td>
                   		<td>操作</td>
                    </tr>
                 <s:iterator value="functionShellList" status="st">  
                    <tr>
                    	<td><s:property value="server_name"/></td>
                        <td><s:property value="shell_function_name"/></td>
                        <td><s:property value="shell_name"/></td>
                        <td><s:property value="shell_paras"/></td>
                        <td><s:property value="shell_out_put"/></td>
                 		<td>
                 			<span class="shell_span default_btn_1" server_id='<s:property value="server_id"/>' server_name='<s:property value="server_name"/>'   shell_function_name='<s:property value="shell_function_name"/>'  shell_id='<s:property value="id"/>' warn_types='<s:property value="warn_types"/>' shell_out_put='<s:property value="shell_out_put"/>' shell_paras='<s:property value="shell_paras"/>' >修改</span>
                 			<span class="default_btn_1" onclick="deleteVo(<s:property value="id"/>);">删除</span>
                 		</td>
                 	</tr>
				 </s:iterator>                    
                </table>
                <div class="page_div">
                    共 <span id="allcount"><s:property value="countData"/></span> 条记录 &nbsp;	每页 <span id="pagesize" ><s:property value="pageSize"/></span> 条记录&nbsp;
					共 <span id="pagecount" ><s:property value="countPage"/></span> 页&nbsp;
					第 <span id="pageno" ><s:property value="gotoPage"/></span> 页&nbsp;&nbsp;
					<input type="button" class="wbt" value="首页" onclick="if(g('gotoPage').value!=g('firstPage').value){g('gotoPage').value=g('firstPage').value;selectData();}else{alert('已经在首页');}"/>
					&nbsp;
					<input type="button" class="wbt" value="上一页" onclick="if(g('gotoPage').value!=g('firstPage').value){g('gotoPage').value=g('prePage').value;selectData();}else{alert('已经在首页');}" />
					&nbsp;
					<input type="button" class="wbt" value="下一页" onclick="if(g('gotoPage').value != g('countPage').value){g('gotoPage').value=g('nextPage').value;selectData();}else{alert('已经在末页');}" />
					&nbsp;
					<input type="button" class="wbt" value="末页" onclick="if(g('gotoPage').value != g('lastPage').value){g('gotoPage').value=g('lastPage').value;selectData();}else{alert('已经在末页');}" />
					&nbsp;&nbsp;
                    
            	</div>
            </div>
    </div>        
	<div id="SHOW_DIV_2" style="display:none;">
        <div class="show_main">
            <div class="title">编辑应用功能点</div>
            <div class="content">
              <s:form id="uploadForm" name="uploadForm" action="uploadShell"  method ="POST" enctype="multipart/form-data">  
              	<input type="hidden"  name="shell_id" id="shell_id"  />
              	<input type="hidden"  name="server_name" id="server_name"  />
              	<input type="hidden"  name="shell_paras" id="shell_paras"  />
              	<input type="hidden"  name="warn_types" id="warn_types"  />
                <ul>
                	 <li>
                        <span class="spanTxt">所属应用：</span>
                	 	<s:select theme="simple" name="server_id" id="server_id" list="#session.monitorServerList"  listKey="id"  listValue="server_name"   headerKey="" headerValue="--请选择应用--" />
                	</li>
                	<li>
                        <span class="spanTxt">功能点名称：</span>
                        <input type="text" class="inputText_1" name="shell_function_name" id="shell_function_name"  />
                    </li>
                    <li id="myFile_li">
                    	<span class="spanTxt">&nbsp;&nbsp;&nbsp;&nbsp;</span>
        				<s:file name ="myFile"  theme="simple"/> 
                    </li>
                    <li>
                    	<span class="spanTxt">&nbsp;&nbsp;&nbsp;&nbsp;</span>
                        <span class="default_btn_2" name="add_but" id="add_but" >添加脚本参数</span>
                    </li>
                    <li>
                        <span class="spanTxt">脚本输出：</span>
                        <input type="text" class="inputText_1" name="shell_out_put" id="shell_out_put"  />
                    </li>
                    <li>
                        <span class="spanTxt">报警机制：</span>
                        			  <input type="checkbox" name="warn_type"  value="1" />邮件提醒
                         ,&nbsp;&nbsp;<input type="checkbox" name="warn_type"  value="2" />短信提醒
                    </li>
                    <br>
                </ul>
      			</s:form>
            </div>
            <div class="bottom">
                <span class="default_btn_1" id="upload-but"  onclick=""   >确定</span>&nbsp;&nbsp;&nbsp;&nbsp;
                <span class="default_btn_1 close">取消</span>
            </div>
        </div>
	</div>     
<script language="javascript">


	 function selectData()
	 {	
	 	$("#selectForm").submit();
	 }

	function clearFormValue(fId){
		$('#'+fId)[0].reset();
		$('.para_li').remove();
	}
	
	function deleteVo(id){
		var url = "/commonSys/manager/monitor/deleteMonitorServerShell.shtml";
		var param = {id:id};
		$.post(url 
			,param 
				,function(rs) {
					if(rs.code==0){
						selectData();
					}
				}
		   ,'json');
	}
	
	function toUploadDiv(){
	
		clearFormValue('uploadForm');
		$("#myFile_li").show();
		
		$("#SHOW_DIV_2").lightbox_me({
			 centered: false
			,modalCSS:{top:'20px'}
			,onClose:function(){clearFormValue('uploadForm');}
		});
		
		$('#upload-but').attr("onclick","uploadShell();");
	}
	
	
	function uploadShell(){
		
		$('#uploadForm')[0].shell_paras.value = getParaJsonData();
		$('#uploadForm')[0].warn_types.value = getCheckBoxValue('warn_type');
		$('#uploadForm')[0].server_name.value = document.getElementById("server_id").options[document.getElementById("server_id").selectedIndex].text; 
		var options = {
			dataType:"html",
			success: process
		};
		
		$('#uploadForm').ajaxSubmit(options);
	}
	
	
	function process(m) 
	{
		rs = eval('('+m+')');
		if(rs.code==0){
			alert('上传成功');
			clearFormValue('uploadForm');
			selectData();
		}else{
			alert('上传失败');
		}
		return;
	}
	
	
	$(function(){
		
		$('#add_but').click(function(){
			var ___li = $('<li class="para_li"><span class="spanTxt">输入参数：</span>&nbsp;<input type="text" class="inputText_1 shell_input_value" name="vo.server_name" id="vo.server_name"  />&nbsp;&nbsp;<span class="close_but shouxing" >X</span></li>');
			$('#add_but').parent().before(___li);  
		});
		
		
		$('.close_but').live('click',function(){
			$(this).parent().remove();
		});
		
		$('.shell_span').live('click',function(){
			toUpdateShellDiv(this);
		});
		
	})
	
	
	function getParaJsonData(){
		var str = '';
	 	var isSuccess = false;
	 	$('.shell_input_value').each(function(i) {
	 		isSuccess = false;
 			var temp = '"para'+(i+1)+'":'+'"'+$(this).val()+'"';
 			var dou = '';
 			if(i==0){
 				str = str+temp;
 			}else{
 				str = str+','+temp;
 			}
 			isSuccess = true;
	 	});
	 	
	 	if(isSuccess){
			return '{'+str+'}';
	 	}else{
	 		return '';
	 	}
	}
	
	
	function toUpdateShellDiv(obj){
		
		clearFormValue('uploadForm');
		$("#myFile_li").hide();
		
		$('#server_id').attr('value',$(obj).attr('server_id')).attr('selected',true).attr('disabled',true);
		$('#shell_function_name').val($(obj).attr('shell_function_name'));
		
		$('#shell_id').val($(obj).attr('shell_id'));
		var shell_paras = $(obj).attr('shell_paras');
		var json = eval( '(' + shell_paras + ')');
		for(var o in json){
			var ___li = $('<li class="para_li"><span class="spanTxt">输入参数：</span>&nbsp;<input type="text" class="inputText_1 shell_input_value" name="vo.server_name" id="vo.server_name" value="'+json[o]+'"  />&nbsp;&nbsp;<span class="close_but shouxing" >X</span></li>');
			$('#add_but').parent().before(___li);  
		}
		
		$('#shell_out_put').val($(obj).attr('shell_out_put'));
		
		var array = $(obj).attr('warn_types').split(',');
		setCheckBoxValue(array,'warn_type');
		
		
		$("#SHOW_DIV_2").lightbox_me({
			 centered: false
			,modalCSS:{top:'20px'}
			,onClose:function(){clearFormValue('uploadForm');}
		});
		
		$('#upload-but').attr("onclick","toUpdateShell();");

	}
	
	
	function toUpdateShell(){
		
		var url = "/commonSys/manager/monitor/updateShell.shtml";
		$('#uploadForm')[0].shell_paras.value = getParaJsonData();
		$('#uploadForm')[0].warn_types.value = getCheckBoxValue('warn_type');
		var param = $("#uploadForm").serialize();
		$.post(url 
			,param 
				,function(rs) {
					if(rs.code==0){
						selectData();
					}
				}
		   ,'json');
		
		clearFormValue('uploadForm');
	}
	
	
	function getCheckBoxValue(checkName) {
    	var str = "";
    	$("input[name="+checkName+"]:checked").each(function(index) {
   			str += $(this).val()+',';
    	});
    	
    	if(str.lastIndexOf(',')==str.length-1){
				str = str.substring(0,(str.length-1));
			}
		return str;    
    }
	
	function setCheckBoxValue(array,checkName) {
		$.each(array, function(i, n){
			 $("input:checkbox[name='"+checkName+"'][value='"+n+"']").attr('checked','true');
		});
    }
	

</script>        	
</body>
</html>


